//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.IO;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.Controls;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using CommunityServer.Galleries.Components;
using CommunityServer.Galleries.Controls;
using FormLabel = CommunityServer.ControlPanel.Controls.FormLabel;
using HelpIcon = CommunityServer.ControlPanel.Controls.HelpIcon;
using ResourceControl = CommunityServer.ControlPanel.Controls.ResourceControl;
using ResourceLinkButton = CommunityServer.ControlPanel.Controls.ResourceLinkButton;

namespace CommunityServer.ControlPanel.PhotoAdmin
{
	/// <summary>
	/// Summary description for SkinOptionsPage.
	/// </summary>
	public class SkinOptionsPage : BaseGalleryAdminPage
	{

		#region Child Controls

		protected ResourceControl Resourcecontrol2;
		protected CheckBox enableThemes;
        protected DropDownList defaultTheme;
        protected FormLabel Formlabel3;
		protected FormLabel Formlabel4;
		protected ResourceControl ResourceControl1;
		protected MPContent DescriptionRegion;
		protected MPContent TaskRegion;
		protected MPContainer Mpcontainer1;
		protected HelpIcon Helpicon1;
		protected HelpIcon Helpicon3;
		protected FileOnlyStatusMessage FOStatus;
		protected ConfigOKStatusMessage Status;
		protected PlaceHolder OptionHolder;
		protected CommunityServer.ControlPanel.Controls.HelpIcon Helpicon4;
		protected CommunityServer.ControlPanel.Controls.FormLabel Formlabel1;
		protected System.Web.UI.WebControls.DropDownList baseTheme;
		protected ResourceLinkButton SaveButton;

		#endregion

		private void Page_Load(object sender, EventArgs e)
		{
			if(!IsPostBack)
			{
				GalleryConfiguration config = GalleryConfiguration.Instance(false);
				
				if(config.FileOnly)
				{
					FOStatus.Visible = true;
					OptionHolder.Visible = false;
				}

				enableThemes.Checked = config.EnableThemes;

                PopulateDefaultThemes();
				
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
			this.SaveButton.Click +=new EventHandler(SaveButton_Click);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion



        private void PopulateDefaultThemes()
        {
            defaultTheme.Items.Clear();

            string dirPath = Context.Server.MapPath(string.Concat(SiteUrls.Instance().Locations["Themes"], "/Galleries"));

            DirectoryInfo dirAllThemes = new DirectoryInfo(dirPath);
            foreach (DirectoryInfo dirTheme in dirAllThemes.GetDirectories())
            {
                DirectoryInfo dirStyle = new DirectoryInfo(Path.Combine(dirTheme.FullName, "Style"));
                if (dirStyle.Exists)
                {
                    defaultTheme.Items.Add(new ListItem(dirTheme.Name, dirTheme.Name.ToLower() + "@"));
                    foreach (FileInfo fi in dirStyle.GetFiles("*.css"))
                    {
                        if (string.Compare(fi.Name, "print.css", true) != 0)
                        {
                            string name = Path.GetFileNameWithoutExtension(fi.FullName).ToLower();
                            if (name != "style")
                            {
                                string themeText = string.Format("{0} - {1}", dirTheme.Name, name);
                                string themeValue = string.Format("{0}@{1}.css", dirTheme.Name, name);
                                defaultTheme.Items.Add(new ListItem(themeText, themeValue.ToLower()));
                            }
                        }
                    }

                }
            }

            string theme = string.Format("{0}@{1}", GalleryConfiguration.Instance().DefaultTheme, GalleryConfiguration.Instance().DefaultSecondaryCSS).ToLower().Trim();

            ListItem liTheme = defaultTheme.Items.FindByValue(theme);
            if (liTheme != null)
                liTheme.Selected = true;

        }

		private void SaveButton_Click(object sender, EventArgs e)
		{
			if(!Page.IsValid)
				return;

			GalleryConfiguration config = GalleryConfiguration.Instance(false);

			if(config.FileOnly)
				return;

			config.EnableThemes = this.enableThemes.Checked;

            string[] theme = defaultTheme.SelectedValue.Split('@');

            config.DefaultTheme = theme[0];

            if (theme.Length > 1)
                config.DefaultSecondaryCSS = theme[1];
            else
                config.DefaultSecondaryCSS = null;

			config.Save();

            CSCache.Remove(ThemeManager.CacheKey);
		    
			Status.Visible = true;

		}

	}
}
